package org.vartist.atld.util.log;

import java.io.IOException;

import android.os.Debug;
import android.util.Log;

public class  LogUtil {
	
	private static void writeToFile(String level, String log){
	    LogToFile logToFile = null;
	    
		try {
			logToFile = new LogToFile();
			logToFile.println("[" + level + "]" + log);

		} catch (IOException e) {
			Log.e(LogConfig.DEBUG_APP_NAME, e.getLocalizedMessage());
		} finally {
			if (logToFile != null)
				try {
					logToFile.close();
				} catch (IOException e) {
					Log.e(LogConfig.DEBUG_APP_NAME, e.getLocalizedMessage());
				}
		}
	}
	
	public static void v(String message)
	{
		if (LogConfig.DEBUG){
			String tag = "";
			String temp = new Throwable().getStackTrace()[1].getClassName();
			if(temp != null){
				int lastDotPos = temp.lastIndexOf(".");
				tag = temp.substring(lastDotPos + 1);
			}
			String methodName = new Throwable().getStackTrace()[1].getMethodName();
			int lineNumber = new Throwable().getStackTrace()[1].getLineNumber();
				
			String logText = "[" + tag +"] " + methodName+ "()" + "[" + lineNumber +"]" + " >> " + message;
			if (LogConfig.DEBUG_TO_FILE) {
				writeToFile("verbose", logText);
			} else {
				Log.v(LogConfig.DEBUG_APP_NAME, logText);
			}
		}
	}
	
	public static void i(String message)
	{
		if (LogConfig.DEBUG){
			String tag = "";
			String temp = new Throwable().getStackTrace()[1].getClassName();
			if(temp != null){
				int lastDotPos = temp.lastIndexOf(".");
				tag = temp.substring(lastDotPos + 1);
			}
			String methodName = new Throwable().getStackTrace()[1].getMethodName();
			int lineNumber = new Throwable().getStackTrace()[1].getLineNumber();
				
			String logText = "[" + tag +"] " + methodName+ "()" + "[" + lineNumber +"]" + " >> " + message;
			
			if (LogConfig.DEBUG_TO_FILE) {
				writeToFile("info", logText);
			} else {
				Log.i(LogConfig.DEBUG_APP_NAME, logText);
			}
		}
	}
	
	public static void d(String message)
	{
		if (LogConfig.DEBUG){
			String tag = "";
			String temp = new Throwable().getStackTrace()[1].getClassName();
			if(temp != null){
				int lastDotPos = temp.lastIndexOf(".");
				tag = temp.substring(lastDotPos + 1);
			}
			String methodName = new Throwable().getStackTrace()[1].getMethodName();
			int lineNumber = new Throwable().getStackTrace()[1].getLineNumber();
				
			String logText = "[" + tag +"] " + methodName+ "()" + "[" + lineNumber +"]" + " >> " + message;
			if (LogConfig.DEBUG_TO_FILE) {
				writeToFile("debug", logText);
			} else {
				Log.d(LogConfig.DEBUG_APP_NAME, logText);
			}
		}
	}
	
	public static void w(String message)
	{
		if (LogConfig.DEBUG){
			String tag = "";
			String temp = new Throwable().getStackTrace()[1].getClassName();
			if(temp != null){
				int lastDotPos = temp.lastIndexOf(".");
				tag = temp.substring(lastDotPos + 1);
			}
			String methodName = new Throwable().getStackTrace()[1].getMethodName();
			int lineNumber = new Throwable().getStackTrace()[1].getLineNumber();
				
			String logText = "[" + tag +"] " + methodName+ "()" + "[" + lineNumber +"]" + " >> " + message;
			if (LogConfig.DEBUG_TO_FILE) {
				writeToFile("warn", logText);
			} else {
				Log.w(LogConfig.DEBUG_APP_NAME, logText);
			}
		}
	}

	public static void e(String message)
	{
		if (LogConfig.DEBUG){
			String tag = "";
			String temp = new Throwable().getStackTrace()[1].getClassName();
			if(temp != null){
				int lastDotPos = temp.lastIndexOf(".");
				tag = temp.substring(lastDotPos + 1);
			}
			String methodName = new Throwable().getStackTrace()[1].getMethodName();
			int lineNumber = new Throwable().getStackTrace()[1].getLineNumber();
				
			String logText = "[" + tag +"] " + methodName+ "()" + "[" + lineNumber +"]" + " >> " + message;
			if (LogConfig.DEBUG_TO_FILE) {
				writeToFile("error", logText);
			} else {
				Log.e(LogConfig.DEBUG_APP_NAME, logText);
			}
		}
	}
	
	public static void debugNativeHeap(){
		if (LogConfig.DEBUG){
			String tag = "";
			String temp = new Throwable().getStackTrace()[1].getClassName();
			if(temp != null){
				int lastDotPos = temp.lastIndexOf(".");
				tag = temp.substring(lastDotPos + 1);
			}
			String methodName = new Throwable().getStackTrace()[1].getMethodName();
			int lineNumber = new Throwable().getStackTrace()[1].getLineNumber();
				
			String logText = "[" + tag + "] " + methodName + "()" + "[" + lineNumber +"]" + " >> " 
                    + "NativeHeapSize=" + Debug.getNativeHeapSize() 
                    + " NativeHeapFreeSize=" + Debug.getNativeHeapFreeSize()
                    + " NativeHeapAllocatedSize()=" + Debug.getNativeHeapAllocatedSize();
			
	        if (LogConfig.DEBUG_TO_FILE){
	            writeToFile("debugNativeHeap", logText);
	        }
	        else{
	        	Log.i(LogConfig.DEBUG_APP_NAME, logText);
	        }
		}
	}
}
